var PITCHWISE = PITCHWISE || {};
PITCHWISE.pitchSearch = PITCHWISE.pitchSearch || {};

PITCHWISE.pitchSearch = function (options) {
    'use strict';

    this.results = options.results;
    this.el = {
        'refine':{
            'name':$('#refine-name'),
            'size':$('#refine-size'),
            'minWidth':$('#refine-min-width'),
            'maxWidth':$('#refine-max-width'),
            'minLength':$('#refine-min-length'),
            'maxLength':$('#refine-max-length'),
            'price':$('#refine-max-price'),
            'surface':$('#refine-surface'),
            'floodlit':$('#refine-floodlit'),
            'goalSize':$('#refine-goal-size'),
            'indoor':$('#refine-indoor'),
            'nets':$('#refine-nets'),
            'parking':$('#refine-parking'),
            'changingRooms':$('#refine-changing-rooms'),
            'showers':$('#refine-showers')
        },
        'results':$('.search-result'),
        'renderArea':$('#render-area'),
        'body':$('body')
    };

    console.log('PITCHWISE.pitchSearch.options', options);
    this.load();
};

PITCHWISE.pitchSearch.prototype.events = function () {
    var _this = this;

    for (var a in this.el.refine) {
        $(document).on('keyup blur change', _this.el.refine[a], function () {
            _this.validateResults();
        });
    }
};

PITCHWISE.pitchSearch.prototype.validateResults = function () {

    for (var a in this.results) {
        this.results[a].valid = true;

        this.validateField('name', 'partialMatch', this.results[a], ['name', 'address', 'area', 'city', 'postCode', 'nearest']);
        this.validateField('size', 'match', this.results[a], false);
        this.validateField('surface', 'match', this.results[a], false);
        this.validateField('minWidth', 'above', this.results[a], 'width');
        this.validateField('maxWidth', 'below', this.results[a], 'width');
        this.validateField('minLength', 'above', this.results[a], 'length');
        this.validateField('maxLength', 'below', this.results[a], 'length');
        this.validateField('price', 'below', this.results[a], 'price');
        this.validateField('floodlit', 'match', this.results[a], false);
        this.validateField('goalSize', 'match', this.results[a], false);
        this.validateField('indoor', 'match', this.results[a], false);
        this.validateField('nets', 'match', this.results[a], false);
        this.validateField('parking', 'match', this.results[a], false);
        this.validateField('changingRooms', 'match', this.results[a], false);
        this.validateField('showers', 'match', this.results[a], false);
    }

    this.paint();
};

PITCHWISE.pitchSearch.prototype.validateField = function (field, rule, result, options) {
    var _this = this;

    var refineVal = _this.el.refine[field].val();
    if (result[field] != undefined) {
        var resultVal = result[field];
    }


    var valid = true;
    if (result.valid) {
        switch (rule) {
            case 'match':
                //console.log('matching :"' + refineVal + '" against "' + resultVal +'"');
                if (refineVal != '' && refineVal != resultVal) {
                    valid = false;
                }
                break;
            case 'partialMatch':
                valid = false;

                for (var a in options) {
                    //console.log('partial matching :"' + refineVal + '" against "' + options[a] + '"');
                    if (result[options[a]].toLowerCase().indexOf(refineVal.toLowerCase()) != -1) {
                        valid = true;
                    }
                }
                break;
            case 'above':
                if (refineVal != '' && refineVal > (result[options] * 1.0)) {
                    valid = false;
                }
                break;
            case 'below':
                //console.log('Comparing ' + options + ' :"' + refineVal + '" against "' + result[options] + '"');
                if (refineVal != '' && refineVal < (result[options] * 1.0)) {
                    valid = false;
                }
                break;
            default :
        }

        result.valid = valid;
    }

    if (valid) {
        //console.log('Validation Passed');
    } else {
        //console.log('Validation Failed');
    }
};

PITCHWISE.pitchSearch.prototype.paint = function () {
    var _this = this;

    this.el.results.each(function () {
        $(this).hide();
        if (_this.results[$(this).attr('data-result-id')].valid) {
            $(this).show();
        }
    });
};

PITCHWISE.pitchSearch.prototype.init = function () {
    this.events();
    this.validateResults();
    this.paint();
};